﻿// CListCtrlEx.cpp: 实现文件
//

#include "pch.h"
#include "Draw.h"
#include "CListCtrlEx.h"


// CListCtrlEx

IMPLEMENT_DYNAMIC(CListCtrlEx, CListCtrl)

CListCtrlEx::CListCtrlEx()
{

}

CListCtrlEx::~CListCtrlEx()
{
}

BEGIN_MESSAGE_MAP(CListCtrlEx, CListCtrl)
	ON_NOTIFY_REFLECT(LVN_GETDISPINFO, &CListCtrlEx::OnLvnGetdispinfo)
	ON_NOTIFY_REFLECT(LVN_ODFINDITEM, &CListCtrlEx::OnLvnOdfinditem)
	ON_NOTIFY_REFLECT(LVN_ODCACHEHINT, &CListCtrlEx::OnLvnOdcachehint)
	ON_WM_VSCROLL()
END_MESSAGE_MAP()

void CListCtrlEx::DrawItem(LPDRAWITEMSTRUCT lp)
{
	// 这种写法不需要attach
	CDC* pDC = CDC::FromHandle(lp->hDC);
	CRect rect{ lp->rcItem };
	int nIndex = lp->itemID;
	// 边框和填充
	static CPen pen(PS_SOLID, 1, COLOR_BORDER);
	static CBrush brush(COLOR_SELECT);
	if (lp->itemState & ODS_SELECTED) {
		pDC->SelectObject(&pen);
		pDC->SelectObject(&brush);
		pDC->Rectangle(rect);
	}
	else {
		pDC->FillSolidRect(rect, (nIndex % 2) ? COLOR_NORMAL1 : COLOR_NORMAL2);
	}
	
	auto pHeader = GetHeaderCtrl();
	int nColumnCount = pHeader->GetItemCount();
	CRect r;
	for (int i = 0; i < nColumnCount; i++) {
		GetSubItemRect(nIndex, i, LVIR_BOUNDS, r);
		// 第0列是完整的宽度，需要修改
		if (i == 0) {
			r.right = GetColumnWidth(0);
		}
		CString str = GetItemText(nIndex, i);
		pDC->DrawText(str, r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
		// TRACE(_T("index=%d, column=%d : l=%d, t=%d, r=%d, b=%d\n"), nIndex, i, r.left, r.top, r.right, r.bottom);
	}
}


// 控件请求数据
void CListCtrlEx::OnLvnGetdispinfo(NMHDR* pNMHDR, LRESULT* pResult)
{
	NMLVDISPINFO* pDispInfo = reinterpret_cast<NMLVDISPINFO*>(pNMHDR);
	
	LV_ITEM* pItem = &(pDispInfo)->item;
	if (pItem->mask & LVIF_TEXT) {
		int row = pItem->iItem;
		auto data = m_dataList.at(row).get();
		CString str;
		switch (pItem->iSubItem) {
		case 0:
			_tcscpy_s(pItem->pszText, pItem->cchTextMax, data->name);
			break;
		case 1:
			str.Format(_T("%d"), data->age);
			_tcscpy_s(pItem->pszText, pItem->cchTextMax, str);
			break;
		case 2:
			str.Format(_T("%d"), data->height);
			_tcscpy_s(pItem->pszText, pItem->cchTextMax, str);
			break;
		}
	}
	// LVIF_IMAGE


	*pResult = 0;
}

// 查找数据
void CListCtrlEx::OnLvnOdfinditem(NMHDR* pNMHDR, LRESULT* pResult)
{
	LPNMLVFINDITEM pFindInfo = reinterpret_cast<LPNMLVFINDITEM>(pNMHDR);
	// 目前按键时会触发
	*pResult = 0;
}

// 缓冲数据
void CListCtrlEx::OnLvnOdcachehint(NMHDR* pNMHDR, LRESULT* pResult)
{
	LPNMLVCACHEHINT pCacheHint = reinterpret_cast<LPNMLVCACHEHINT>(pNMHDR);
	
	TRACE(_T("缓存提示: %d - %d\n"), pCacheHint->iFrom, pCacheHint->iTo);

	*pResult = 0;
}


void CListCtrlEx::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
	CListCtrl::OnVScroll(nSBCode, nPos, pScrollBar);

	int currentPos = GetScrollPos(SB_VERT);
	if (currentPos >= GetItemCount() - GetCountPerPage() - 5) {
		TRACE(_T("加载新的一批\n"));
		int newIndex = m_dataList.size();
		CString str;
		for (int i = 0; i < 10; i++) {
			str.Format(_T("王五 + %d"), newIndex + i);
			m_dataList.emplace_back(std::make_unique<MyS>(str, 10 + i, 110 + i));// 直接构造到vector中
		}
		SetItemCount(m_dataList.size());
		RedrawItems(newIndex, newIndex + 10 - 1);
	}
}
